<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>设备信息</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
 </head>
 <body>
  <t:formvalid formid="formobj" dialog="true" usePlugin="password" layout="table" action="userDoorAccessController.do?doAdd" tiptype="3"   beforeSubmit="getIds()">
		<table style=" " cellpadding="0" cellspacing="1" class="formtable">
			<tr>
					<td align="right">
						<label class="Validform_label">
							用户类型:
						</label>
					</td>
					<td class="value">
						<input type="radio" name="userType" checked="checked" value="0"/>教师
						<input type="radio" name="userType" value="1"/>学生
					</td>	
				</tr>	
				<tr>
					<td align="right" id="teacher">
						<label class="Validform_label">
							教师:
						</label>
					</td>
					<td align="right" id="stu" style="display: none;">
						<label class="Validform_label">
							学生:
						</label>
					</td>
					<td class="value">
						<div style="padding-top:5px;height: 300px;">
							<a class='myBtn roundBorder' onclick="addTeacher()" id="teachBtn">添加老师</a>
							<a class='myBtn roundBorder' onclick="addStudent()" id="stuBtn" style="display: none;">添加学生</a>
							<table class="commonTable" id='dataTable'>
								<thead>
									<tr>
										<th>姓名</th>
										<th>工号</th>
										<th>操作</th>
									</tr>
								</thead> 
								<tbody>
								</tbody>
							</table> 
						</div>
					</td>	
				</tr>	
				<tr>
					<td align="right">
						<label class="Validform_label">
							授权类型:
						</label>
					</td>
					<td class="value">
					     	<t:enumSelect field="accessRight" type="radio" defaultVal="0" 
								  hasLabel="false" enumClass="com.mengtian_tech.smart_lab.entity.doorAccess.AccessRight"
								  title="授权类型">
							</t:enumSelect>     
							<span class="Validform_checktip"></span>
							<label class="Validform_label" style="display: none;">授权类型</label>
					</td>
				</tr>
				<tr id="start" style="display: none;">
					<td align="right">
						<label class="Validform_label">
							开始时间:
						</label>
					</td>
					<td class="value">
						<input id="startDate" name="startDate" type="text"  maxlength="80"
					     	 style="width: 150px" class="inputxt">
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">开始时间</label>
					</td>
				</tr>
				<tr id="end" style="display: none;">
					<td align="right">
						<label class="Validform_label">
							结束时间:
						</label>
					</td>
					<td class="value">
						<input id="endDate" name="endDate" type="text"  maxlength="80"
					     	 style="width: 150px" class="inputxt">	
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">结束时间</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							门禁:
						</label>
					</td>
					<td class="value"> 
                	     <input id="doorAccessId" name="doorAccessId" type="hidden"  >               
					     <input id="doorAccessName"  type="text" style="width: 150px" class="inputxt" readonly="readonly" datatype="*">
					     <t:choose textname="name" inputTextname="doorAccessName" hiddenName="doorAccessId" hiddenid="id" url="doorAccessController.do?goSelect" name="list"
                          icon="icon-search" title="选择门禁"  isclear="false" isInit="true" width="470px"></t:choose>
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">门禁</label>						
					</td>
				</tr>	
			</table>
			<input type="hidden" name="ids">
		</t:formvalid>
<script type="text/javascript">
	$("#startDate").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'});});
	$("#endDate").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'});});
	$(function(){
		$("[name='userType']").on('click',function(){
			if($(this).val()=='0'){
				$("#teacher").show();
				$("#stu").hide();
				
				$("#teachBtn").show();
				$("#stuBtn").hide();
			}else{
				$("#stu").show();
				$("#teacher").hide();
				
				$("#stuBtn").show();
				$("#teachBtn").hide();
			}
			ids = [];
			$('#dataTable tbody').empty();
		});
		
		$("[name='accessRight']").on('click', function(){
			if($(this).val()=='0'){
				$("#start").hide();
				$("#end").hide();
				$("#startDate").attr('dataType',"");
				$("#startDate").removeAttr('dataType');
				$("#endDate").removeAttr('dataType');
			}else{
				$("#start").show();
				$("#end").show();
				$("#startDate").attr('dataType',"*");
				$("#endDate").attr('dataType',"*");
			}
		});
	});

  	function addTeacher(){
	  	var url = 'teacherController.do?goSelect';
  		$.dialog({
  			id:'dg',
  			content: 'url:' + url,
  			lock : true,
  			zIndex: 4000, 
  			width:500,
  			height:280,
  			title:'选择教师', 
  			opacity : 0.3,
  			cache:false,
  			ok: function(){
  				iframe = this.iframe.contentWindow;
  				var rows = iframe.getData();
  				addRows(rows);
  				return true;	
  			},
  			cancelVal: '关闭',
  			cancel: true /*为true等价于function(){}*/,
  		});	
  	}
  	
  	function addStudent(){
	  	var url = 'studentController.do?goSelect';
  		$.dialog({
  			id:'dg',
  			content: 'url:' + url,
  			lock : true,
  			zIndex: 4000, 
  			width:500,
  			height:280,
  			title:'选择学生', 
  			opacity : 0.3,
  			cache:false,
  			ok: function(){
  				iframe = this.iframe.contentWindow;
  				var rows = iframe.getData();
  				addRows(rows);
  				return true;	
  			},
  			cancelVal: '关闭',
  			cancel: true /*为true等价于function(){}*/,
  		});	
  	}
  
  	var ids = [];
  	function addRows(rows){
		for(var i=0;i<rows.length;i++){
			var row = rows[i];
			var id = row.id;
			
			var index = ids.indexOf(''+id);
			if(index!=-1) continue;
			
			var html = '<tr>';
			html += 
				'<td>'+row.realName+'</td>'+
				'<td>'+ row.staffNo +'</td>'+
				'<td>'+
					'<a href="javascript:void(0)" onclick="deleteRow(this,\''+ row.id +'\')">删除</a>'+
				'</td>';
			//添加一行
			$('#dataTable tbody').append(html+"</tr>");
			
			ids.push(row.id+'');
		}
	}
  	
  	function deleteRow(obj, id){
  		console.log(ids);
  		$(obj).parent().parent().remove();
  		
  		var index = ids.indexOf(''+id);
		if(index!=-1){
			ids.splice(index,1);
		}
		console.log(ids);
  	}
  	
  	function getIds(){
  		$("[name='ids']").val(ids);
  		if($("[name='accessRight']:checked").val()!='1'){
  			$("#startDate").val('');
			$("#endDate").val('');
  		}else{
  			if($("#startDate").val()>$("#endDate").val()){
  				tip('开始时间不能晚于结束时间');			
	  			return false;
  			}
  		}
  	}
  </script>
 </body>